package com.micai.springboot.base.bean;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/6 14:07
 * @Description: Spring上下文工具类
 */
@Component
public class ApplicationContextHolder implements ApplicationContextAware {

    static ApplicationContext applicationContext;

    public ApplicationContextHolder() {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> t) {
        return applicationContext.getBean(t);
    }

    public static <T> Map<String, T> getBeansOfType(Class<T> t) {
        return applicationContext.getBeansOfType(t);
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }
}
